/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtwitt.client.gui;

import java.io.IOException;
import jtwitt.server.model.AppUser;
import jtwitt.socket.ClientThread;
import jtwitt.util.Actions;

/**
 * The register form used to create a new account
 * @author M
 */
public class Register extends javax.swing.JDialog {
    MainClient gui;
    ClientThread cThread;
    public AppUser appUser;
    public String username;
    public String password;
    public String firstName;
    public String lastName;
    public String email;
    
    /**
     * Creates new form Register
     */
    public Register(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jLabelError.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextEmail = new javax.swing.JTextField();
        jTextFirstName = new javax.swing.JTextField();
        jTextLastName = new javax.swing.JTextField();
        jTextUsername = new javax.swing.JTextField();
        jTextPassword = new javax.swing.JTextField();
        jButtonRegister = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabelRegister = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelError = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextServerIP = new javax.swing.JTextField();
        jTextPort = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("E-mail:");

        jLabel2.setText("Fisrt Name:");

        jLabel3.setText("Last Name:");

        jLabel4.setText("Username:");

        jLabel5.setText("Password:");

        jButtonRegister.setText("Register");
        jButtonRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegisterActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Close");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jLabelRegister.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabelRegister.setText("Fill the information to register");

        jLabelError.setText("jLabel6");

        jLabel6.setText("Server IP:");

        jLabel7.setText("Port Number:");

        jTextServerIP.setText("localhost");

        jTextPort.setText("1234");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButtonRegister)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonCancel))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextUsername)
                                    .addComponent(jTextEmail)
                                    .addComponent(jTextFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                                    .addComponent(jTextPassword)
                                    .addComponent(jTextLastName, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTextServerIP, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                                    .addComponent(jLabelError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jTextPort, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jLabelRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextServerIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jLabelError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRegister)
                    .addComponent(jButtonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegisterActionPerformed
        
        
        if("".equals(jTextEmail.getText()) || "".equals(jTextFirstName.getText()) || "".equals(jTextLastName.getText())
            || "".equals(jTextUsername.getText()) || "".equals(jTextPassword.getText()) || "".equals(jTextServerIP.getText())
                || "".equals(jTextPort.getText())){// check if there's a textbox empty and notify the user to fill missing info
            
            jLabelError.setText("Cannot save null values, please correct your information");
            jLabelError.setVisible(true);

        }
        else{
             try{
                int connected=-1;
                
                lockControls();
                
                //try to create a connection
                connected=gui.startThreads(jTextServerIP.getText(),Integer.parseInt(jTextPort.getText()));
                    
                if(connected == -1){//on connect error
                             jLabelError.setText("Problem connecting to server: "+jTextServerIP.getText()+" on port:"+jTextPort.getText());

                            unlockControls();
                            jButtonRegister.setEnabled(true);  
                            jTextServerIP.setEnabled(true);
                            jTextPort.setEnabled(true);
                    }
                else if(connected ==4){//if there's an active connection
                            jLabelError.setText("Connected to server: "+jTextServerIP.getText()+" on port:"+jTextPort.getText());
                            jLabelError.setVisible(true);
                            //if connected , user cannot change connection parameters
                            jTextServerIP.setEnabled(false);
                            jTextPort.setEnabled(false);
                }
                }
                catch(NumberFormatException | IOException ex){
                            jLabelError.setText("Problem connecting to server: "+ex);
                            jLabelError.setVisible(true);
                            jButtonRegister.setEnabled(true);
                            unlockControls();
                }
             
                         
                appUser = new AppUser();
                appUser.setEmail(jTextEmail.getText());
                appUser.setFirstName(jTextFirstName.getText());
                appUser.setLastName(jTextLastName.getText());
                appUser.setUsername(jTextUsername.getText());
                appUser.setPassword(jTextPassword.getText());

                cThread = gui.getCThread();

                cThread.setRegister(this);
                cThread.setAction(Actions.REGISTER);
                cThread.wakeMe();

        }
        
    }//GEN-LAST:event_jButtonRegisterActionPerformed
    
    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonRegister;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabelError;
    private javax.swing.JLabel jLabelRegister;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextEmail;
    private javax.swing.JTextField jTextFirstName;
    private javax.swing.JTextField jTextLastName;
    private javax.swing.JTextField jTextPassword;
    private javax.swing.JTextField jTextPort;
    private javax.swing.JTextField jTextServerIP;
    private javax.swing.JTextField jTextUsername;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets the result of the register action
     * @param accountSaved integer that indicates whether  the account has been saved (1) or not (-1)
     */
    public void accountSaved(int accountSaved)
{
    if(accountSaved == 1){
         
          jLabelError.setText("Your account has been created.");
          jLabelError.setVisible(true);
          
          
    }
    else{
        
        jLabelError.setText("Error, User already exists please try again using different data.");
        jLabelError.setVisible(true);
        jButtonRegister.setEnabled(true);
        unlockControls();
    
    }
}

public void setMainClient(MainClient gui){
    this.gui = gui;
}

    /**
     * Disable controls until actions performed
     */
    
private void lockControls(){

    jTextEmail.setEnabled(false);
    jTextFirstName.setEnabled(false);
    jTextLastName.setEnabled(false);
    jTextPassword.setEnabled(false);
    jTextPort.setEnabled(false);
    jTextServerIP.setEnabled(false);
    jTextUsername.setEnabled(false);
    jButtonRegister.setEnabled(false);
     repaint();
    
}

     /**
     * Re-enable controls after actions performed
     */
private void unlockControls(){
    
    jTextEmail.setEnabled(true);
    jTextFirstName.setEnabled(true);
    jTextLastName.setEnabled(true);
    jTextPassword.setEnabled(true);
    jTextPort.setEnabled(true);
    jTextServerIP.setEnabled(true);
    jTextUsername.setEnabled(true);
     repaint();
}
            

}
